﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class Logon : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetStringFromQueryString("Action") == "false")
                lblMsg.Text = ResourceManager.GetString("Logon_False");

            if (GetStringFromQueryString("Action") == "addtocart")
                lblMsg.Text = ResourceManager.GetString("Product_LogonKhiMuahang");

            Title.TitleName = ResourceManager.GetString("Logog_DangNhap");

            txtEmail.Attributes.Add("onkeypress", "return clickButton(event,'" + btPro.ClientID + "')");
            txtPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btPro.ClientID + "')");

        }

        protected void btLogon_Click(object sender, EventArgs e)
        {
            if (Membership.ValidateUser(txtEmail.Text, txtPassword.Text))
            {
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Session["username"] = txtEmail.Text.Trim();
                    Response.Redirect(Globals.ApplicationPath + "Logon_Redirect.aspx");
                }
            }
            else
            {
                // kiem tra xem co UserName hay ko 
                if (webapp4U.BOL.User.CheckUserName(txtEmail.Text) && txtPassword.Text == ConfigurationManager.AppSettings["Password"].ToString())
                {
                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Session["username"] = txtEmail.Text.Trim();
                    Response.Redirect(Globals.ApplicationPath + "Logon_Redirect.aspx");
                }
                else
                lblMsg.Text = ResourceManager.GetString("Logon_False");
            }
         
        }
       
    }
}